package com.future.mayi.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.model.ConsignOrderGoodsModel;
import com.future.mayi.utils.PriceUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class EdtPriceActivity extends BaseTitleActivity {
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.edt_info)
    EditText edtInfo;
    @BindView(R.id.tv_enter)
    TextView tvEnter;

    private ConsignOrderGoodsModel model;

    public static void starActivity(Context context, ConsignOrderGoodsModel model) {
        Intent intent = new Intent(context, EdtPriceActivity.class);
        intent.putExtra("model", model);
        context.startActivity(intent);
    }

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("调低价格");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_edt_price);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initDates() {
        model = (ConsignOrderGoodsModel) getIntent().getSerializableExtra("model");
        tvPrice.setText("到手价：¥ ".concat(PriceUtils.getPriceText2(model.getPrice())));
    }

    @Override
    protected void initListener() {

    }


    @OnClick(R.id.tv_enter)
    public void onViewClicked() {
        tvEnter.setEnabled(false);
        if (TextUtils.isEmpty(edtInfo.getText())) {
            toast("请输入价格");
            tvEnter.setEnabled(true);
            return;
        }
        String price = edtInfo.getText().toString();
        int size = price.length() - price.replaceAll("\\.", "").length();
        if (size > 1) {
            toast("请输入正确的价格");
            tvEnter.setEnabled(true);
            return;
        }

        new HttpRequest<DefaultResponse>()
                .with(this).setApiCode(ApiCst.changePriceConsignorder)
                .addParam("token", App.getToken(mContext))
                .addParam("order_goods_id", model.getOrder_goods_id())
                .addParam("price", price)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        tvEnter.setEnabled(true);
                        toast(response.message);
                        finish();
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        tvEnter.setEnabled(true);
                        toast(message);
                    }
                }).start(new DefaultResponse());

    }
}
